package com.woniuxy.handler;

import com.woniuxy.util.smsutil.RegexpUtil;
import com.woniuxy.util.smsutil.Sms;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 〈短信服务接口〉
 *
 * @author mjh
 * @create 2019/9/24
 */

@RestController
@RequestMapping("/sms")
@Api(value = "短信接口", tags = "用户")
public class SmsHandler {

    @GetMapping("/sendcode/{phone}")
    @ApiOperation(value = "验证码发送", notes = "传入电话号码，发送验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号码")
    })
    public String sendCode(HttpServletRequest request, @PathVariable("phone") String phone) {
        String result = "发送验证码失败";
        //数据验证
        if (!phone.matches(RegexpUtil.REGEX_MOBILE)) {
            result = "账号格式有误，请重新输入";
            return result;
        }
        if (Sms.getCode(request, phone)) {
            result = "发送验证码成功";
        }
        System.out.println(result);
        return result;
    }

    @GetMapping("/sendtime/{phone}/{time}/{good}")
    @ApiOperation(value = "竞拍商品通知短信发送", notes = "传入电话号码，开拍时间，商品名发送通知短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "time", value = "开拍时间"),
            @ApiImplicitParam(name = "good", value = "商品名")
    })
    public String sendTime(@PathVariable("phone") String phone, @PathVariable("time") String time, @PathVariable("good") String good) {
        String result = "发送竞拍商品通知失败";
        //数据验证
        if (!phone.matches(RegexpUtil.REGEX_MOBILE)) {
            result = "账号格式有误，请重新输入";
            return result;
        }
        if (Sms.noticeTime(phone, time, good)) {
            result = "发送竞拍商品通知成功";
        }
        System.out.println(result);
        return result;
    }

    @GetMapping("/sendsuccess/{phone}/{good}")
    @ApiOperation(value = "竞拍成功短信通知", notes = "传入电话号码，商品名，通知用户竞拍成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "电话号码"),
            @ApiImplicitParam(name = "good", value = "商品名")
    })
    public String sendSuccess(@PathVariable("phone") String phone, @PathVariable("good") String good) {
        String result = "竞拍成功短信发送失败";
        //数据验证
        if (!phone.matches(RegexpUtil.REGEX_MOBILE)) {
            result = "账号格式有误，请重新输入";
            return result;
        }
        if (Sms.noticeSuccess(phone, good)) {
            result = "竞拍成功短信发送成功";
        }
        System.out.println(result);
        return result;
    }
}